package collection;

import java.util.Arrays;
import java.util.List;

/**
 * 数组转换为集合
 */
public class ArrayToList {
    public static void main(String[] args) {
        String[] array = {"one","two","three","four","five"};
        System.out.println("array:"+ Arrays.toString(array)); //[one, two, three, four, five]

        //asList()方法会返回内部的ArrayList，内部直接引用给定数组array
        List<String> list = Arrays.asList(array);
        System.out.println("list:"+list); //[one, two, three, four, five]

        //修改数组中元素数据后，集合也会相应的改变
        array[1] = "six";
        System.out.println("array:"+Arrays.toString(array)); //[one, six, three, four, five]
        System.out.println("list:"+list); //[one, six, three, four, five]

        //修改集合元素数据后，数组也会相应的改变
        list.set(2,"seven");
        System.out.println("list:"+list); //[one, six, seven, four, five]
        System.out.println("array:"+Arrays.toString(array)); //[one, six, seven, four, five]

        //给集合添加/删除元素相当于给数组添加/删除元素
        //而数组是定长的，不会自动扩容/缩容，因此发生不支持操作异常
        //list.add("!!!"); //运行时会发生不支持操作异常

    }
}











